package com.jincp.image.load.glide_loder;

import android.support.annotation.NonNull;
import android.util.Log;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.HttpException;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.data.DataFetcher.DataCallback;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.util.ContentLengthInputStream;
import com.bumptech.glide.util.Preconditions;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map.Entry;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.Call.Factory;
import okhttp3.Request.Builder;

public class OkHttpStreamFetcher implements DataFetcher<InputStream>, Callback {
    private static final String TAG = "OkHttpFetcher";
    private final Factory client;
    private final GlideUrl url;
    private InputStream stream;
    private ResponseBody responseBody;
    private DataCallback<? super InputStream> callback;
    private volatile Call call;

    public OkHttpStreamFetcher(Factory client, GlideUrl url) {
        this.client = client;
        this.url = url;
    }

    public void loadData(@NonNull Priority priority, @NonNull DataCallback<? super InputStream> callback) {
        Builder requestBuilder = (new Builder()).url(this.url.toStringUrl());
        Iterator request = this.url.getHeaders().entrySet().iterator();

        while(request.hasNext()) {
            Entry headerEntry = (Entry)request.next();
            String key = (String)headerEntry.getKey();
            requestBuilder.addHeader(key, (String)headerEntry.getValue());
        }

        Request request1 = requestBuilder.build();
        this.callback = callback;
        this.call = this.client.newCall(request1);
        this.call.enqueue(this);
    }

    public void onFailure(@NonNull Call call, @NonNull IOException e) {
        this.callback.onLoadFailed(e);
    }

    public void onResponse(@NonNull Call call, @NonNull Response response) {
        this.responseBody = response.body();
        if(response.isSuccessful()) {
            long contentLength = ((ResponseBody)Preconditions.checkNotNull(this.responseBody)).contentLength();
            this.stream = ContentLengthInputStream.obtain(this.responseBody.byteStream(), contentLength);
            this.callback.onDataReady(this.stream);
        } else {
            this.callback.onLoadFailed(new HttpException(response.message(), response.code()));
        }

    }

    public void cleanup() {
        try {
            if(this.stream != null) {
                this.stream.close();
            }
        } catch (IOException var2) {
            ;
        }

        if(this.responseBody != null) {
            this.responseBody.close();
        }

        this.callback = null;
    }

    public void cancel() {
        Call local = this.call;
        if(local != null) {
            local.cancel();
        }

    }

    @NonNull
    public Class<InputStream> getDataClass() {
        return InputStream.class;
    }

    @NonNull
    public DataSource getDataSource() {
        return DataSource.REMOTE;
    }
}
